cmake_minimum_required(VERSION 3.5)


# 开启测试;
include(GoogleTest)
include(FetchContent)

# find_package(GTest REQUIRED)
# find_package(Threads REQUIRED)

FetchContent_Declare(
    googletest
    GIT_REPOSITORY https://mirror.ghproxy.com/https://github.com/google/googletest.git
    GIT_TAG        v1.14.0
  )

FetchContent_GetProperties(googletest)


if(NOT googletest_POPULATED)
    FetchContent_Populate(googletest)

    # Prevent GoogleTest from overriding our compiler/linker options
    # when building with Visual Studio
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    # Prevent GoogleTest from using PThreads
    set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)

    # adds the targers: gtest, gtest_main, gmock, gmock_main
    add_subdirectory(
        ${googletest_SOURCE_DIR}
        ${googletest_BINARY_DIR}
        )
    
        # Silence std::tr1 warning on MSVC
        if(MSVC)
        foreach(_tgt gtest gtest_main gmock gmock_main)
            target_compile_definitions(${_tgt}
            PRIVATE
                "_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING"
            )
        endforeach()
        endif()
endif()

include_directories(${CMAKE_SOURCE_DIR}/include)


set(TestName  unittest)
aux_source_directory(. TestSRCS)

add_executable(${TestName} ${TestSRCS})


target_link_libraries(${TestName}
  PRIVATE
    ${PROJECT_LIB}
    gtest_main
  )

target_link_libraries(${TestName} ${GTEST_BOTH_LIBRARIES})


enable_testing()

add_test(
    NAME ${TestName}
    COMMAND $<TARGET_FILE:${TestName}>
)